package com.zgh.myexamproject;


import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zgh.myexamproject.util.MyDBHelper;
import com.zgh.myexamproject.util.loginInfo;

public class LoginActivity extends AppCompatActivity {
    private EditText et_username,et_pwd;
    private TextView tv_register;
    private String userName,passWord,Psw;
    private Button login;
    private CheckBox save_pwd;
    private SQLiteDatabase db;
    private MyDBHelper myDBHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuyemian);
        myDBHelper = new MyDBHelper(this);
        init();
    }
    public void init(){
        Button login=findViewById(R.id.b1);
        et_username = (EditText) findViewById(R.id.username);
        et_pwd = (EditText) findViewById(R.id.pwd);
        save_pwd = (CheckBox) findViewById(R.id.save_pwd);
        /*db = myDBHelper.getWritableDatabase();
        db.execSQL("DROP TABLE IF EXISTS tb_Game");
        db.execSQL("create table tb_Game(gameid char(10)primary key,gamename varchar(20),gametime varchar(20),creator varchar(20),gamenote varchar(20))");*/
        tv_register = (TextView) findViewById(R.id.register);
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册界面
                Intent intent=new Intent( LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                LoginActivity.this.finish();
            }
        });
        et_username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                getEditString();
                if(!hasFocus && userName!=""){
                    System.out.println("用户名"+userName);
                    if(isrememberPWD(userName)== 1){
                        String rpsw = getPassword(userName);
                        et_pwd.setText(rpsw);
                        save_pwd.setChecked(true);
                    }else{
                        et_pwd.setText("");
                        save_pwd.setChecked(false);
                    }
                }
            }
        });
        login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                getEditString();
                Psw = getPassword(userName);
                if(TextUtils.isEmpty(userName)){
                    Toast.makeText( LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(passWord)){
                    Toast.makeText( LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(Psw!=null &&Psw.equals(passWord)){
                    final boolean CheckBoxLogin = save_pwd.isChecked();
                    System.out.println(CheckBoxLogin);
                    if(CheckBoxLogin){
                        rememberPWD(userName,true);
                    }
                    else{
                        rememberPWD(userName,false);
                    }
                    loginInfo.username = userName;
                    System.out.println("当前用户"+loginInfo.username);
                    Toast.makeText( LoginActivity.this, "welcome！"+ userName, Toast.LENGTH_SHORT).show();
                    Intent data = new Intent();
                    LoginActivity.this.finish();
                    startActivity(new Intent( LoginActivity.this, MainActivity.class));
                    return;
                }else if(Psw!=null &&!Psw.equals(passWord)){
                    Toast.makeText( LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText( LoginActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }
    private void rememberPWD(String username,boolean remember){
        db = myDBHelper.getWritableDatabase();
        if (remember){
            ContentValues values = new ContentValues();
            values.put("isremember",1);
            db.update("tb_User",values,"username=?",new String[]{username});
        }

        else{
            ContentValues values = new ContentValues();
            values.put("isremember",0);
            db.update("tb_User",values,"username=?",new String[]{username});
        }

        db.close();
    }
    private int isrememberPWD(String username){

        db = myDBHelper.getReadableDatabase();
        /*String sql = "DROP TABLE IF EXISTS tb_User";
        db.execSQL(sql);
        db.execSQL("create table tb_User(username char(10)primary key,password varchar(20),isremember INT(2) default 0,nickname varchar(10),sex varchar(10),phone varchar(10),note varchar(20))");*/
        System.out.println(db);
        //Cursor cursor = db.rawQuery("select"+"'"+"isremember"+"'"+" from tb_User where username = ?", new String[]{username});
        Cursor cursor = db.rawQuery("select * from tb_User where username = ?", new String[]{username});
        if(cursor.getCount()!=0){
            cursor.moveToFirst();
            do {
                int isremember = cursor.getInt(cursor.getColumnIndex("isremember"));
                System.out.println("密码"+isremember);
                return isremember;
            }while(cursor.moveToNext());

            //System.out.println("密码"+isremember);
        }
        return 0;
    }
    private boolean isExistUserName(String userName){
        boolean has_userName = true;
        db = myDBHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from tb_User where username = ?", new String[]{userName});
        System.out.println("nihao"+cursor.getCount());
        if(cursor.getCount()==0) {
            has_userName=false;
        }
        cursor.close();
        db.close();
        return has_userName;
    }
    private void getEditString(){
        userName = et_username.getText().toString().trim();
        passWord = et_pwd.getText().toString().trim();
    }

    private String getPassword(String username){
        db = myDBHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select password from tb_User where username = ?", new String[]{username});
        if(cursor.getCount()!=0){
            cursor.moveToFirst();
            String password = cursor.getString(cursor.getColumnIndex("password"));
            System.out.println("密码"+password);
            return password;
        }
       return null;
    }
}